package com.liunian.subject.application.controller;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.liunian.subject.application.dto.SubjectLabelDTO;
import com.liunian.subject.common.entity.Result;
import com.liunian.subject.domain.entity.SubjectLabelBO;
import com.liunian.subject.domain.service.SubjectLabelDomainService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@Api(tags = "刷题标签controller")
@Slf4j
@RequestMapping("/subject/label")
public class SubjectLabelController {

    @Resource
    private SubjectLabelDomainService subjectLabelDomainService;

    @PostMapping("/add")
    @ApiOperation(value = "新增标签")
    public Result<Boolean> add(@RequestBody SubjectLabelDTO subjectLabelDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectLabelController.add.dto:{}", JSON.toJSONString(subjectLabelDTO));
            }
            Preconditions.checkArgument(!StringUtils.isBlank(subjectLabelDTO.getLabelName()), "标签名不能为空");
            SubjectLabelBO bo = subjectLabelDTO.subjectLabelDTOToBO();
            return Result.success(subjectLabelDomainService.add(bo));
        } catch (Exception e) {
            return Result.fail("新增标签失败！");
        }
    }

    @PostMapping("/update")
    @ApiOperation(value = "更新标签")
    public Result<Boolean> update(@RequestBody SubjectLabelDTO subjectLabelDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectLabelController.update.dto:{}", JSON.toJSONString(subjectLabelDTO));
            }
            Preconditions.checkNotNull(subjectLabelDTO.getId(), "标签id不能为空");
            SubjectLabelBO bo = subjectLabelDTO.subjectLabelDTOToBO();
            return Result.success(subjectLabelDomainService.update(bo));
        } catch (Exception e) {
            log.error("SubjectLabelController.update.error:{}", e.getMessage(), e);
            return Result.fail("更新标签失败！");
        }
    }

    @PostMapping("/delete")
    @ApiOperation(value = "删除标签")
    public Result<Boolean> delete(@RequestBody SubjectLabelDTO subjectLabelDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectLabelController.delete.dto:{}", JSON.toJSONString(subjectLabelDTO));
            }
            SubjectLabelBO bo = subjectLabelDTO.subjectLabelDTOToBO();
            return Result.success(subjectLabelDomainService.delete(bo));
        } catch (Exception e) {
            log.error("SubjectLabelController.delete.error:{}", e.getMessage(), e);
            return Result.fail("刪除标签失败！");
        }
    }

    @PostMapping("/queryLabelByCategoryId")
    @ApiOperation(value = "根据分类查询标签")
    public Result<List<SubjectLabelBO>> queryLabelByCategoryId(@RequestBody SubjectLabelDTO subjectLabelDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectLabelController.queryLabelByCategoryId.dto:{}", JSON.toJSONString(subjectLabelDTO));
            }
            Preconditions.checkNotNull(subjectLabelDTO.getCategoryId(), "分类id不能为空");
            SubjectLabelBO bo = subjectLabelDTO.subjectLabelDTOToBO();
            List<SubjectLabelBO> subjectLabelBOList = subjectLabelDomainService.queryLabel(bo);
            return Result.success(subjectLabelBOList);
        } catch (Exception e) {
            log.error("SubjectLabelController.queryLabelByCategoryId.error:{}", e.getMessage(), e);
            return Result.fail("查询分类下标签失败！");
        }
    }



}
